package com.eh.base.web;

import com.eh.base.BaseConstants;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * Created by David Li on 2016/10/18.
 */
public class BaseResponse<T> implements Serializable {
    int status;

    String error;

    String msg;

    T data;

    private BaseResponse() {
    }

    public static BaseResponse success() {
        return success(null);
    }

    public static <T> BaseResponse<T> success(T t) {
        return create(BaseConstants.SC_OK, BaseErrorEnum.OK.getCodeStr(), BaseErrorEnum.OK.getMessage(), t);
    }

    public static <T> BaseResponse<T> error(Throwable t) {
        return create(BaseConstants.SC_FAIL, BaseErrorEnum.FAIL.getCodeStr(), t.getMessage(), null);
    }

    public static <T> BaseResponse<T> error(String message) {
        return create(BaseConstants.SC_FAIL, StringUtils.EMPTY, message, null);
    }

    public static <T> BaseResponse<T> error(String error, String message) {
        return create(BaseConstants.SC_FAIL, error, message, null);
    }

    public static <T> BaseResponse<T> create(int status, String error, String message, T t) {
        BaseResponse response = new BaseResponse();
        response.setStatus(status);
        response.setError(error);
        response.setMsg(message);
        response.setData(t);
        return response;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
